package com.seata.demo.controller;

import com.seata.demo.service.BusinessService;
import com.seata.demo.common.res.Resp;
import io.seata.core.context.RootContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @comments
* @author  zg
* @date 2021年2月9日
*/
@Tag(name = "业务功能")
@AllArgsConstructor
@Slf4j
@Validated
@RestController
@RequestMapping("/biz/")
public class BusinessController {
    
    private BusinessService businessService;

    @Operation(summary = "购买商品")
    @Parameters({
            @Parameter(name = "userId",description = "用户id", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "commodityCode",description = "商品编码",required = true, in = ParameterIn.QUERY),
            @Parameter(name = "commodityCount",description = "商品数量",required = true, in = ParameterIn.QUERY)})
    @PostMapping("/purchase")
    public Resp<?> purchase(
            @NotBlank(message = "userId不能为空")String userId, 
            @NotBlank(message = "商品编码不能为空")String commodityCode, 
            @NotNull(message = "商品数量不能为空") @Min(value = 1, message = "商品数量最小为1") Integer commodityCount,
            HttpServletRequest request) {
        String tx_xid = request.getHeader(RootContext.KEY_XID.toLowerCase());
        log.info("购买商品：tx_xid={}, userId={}, commodityCode={}, commodityCount={}", tx_xid, userId, commodityCode, commodityCount);
        return businessService.purchase(userId, commodityCode, commodityCount);
    }
    
}
